﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace GameEditor.Common
{    

    public partial class ResourceExplorer : DockableWindow
    {
        public enum NodeType
        {
            None = 0,
            Directory,
            Effect,
            Material,
            Particle,
        }

        public class NodeInfo
        {
            public string fileName;
            public NodeType type;

            public NodeInfo()
            {
                fileName = "none";
                type = NodeType.None;
            }
        }



        public ResourceExplorer()
        {
            InitializeComponent();
        }


        public void setTypeName(string typeName)
        {
            mTypeName = typeName;
        }

        public void setShowDirectory(string dirName)
        {
            mDir = dirName;
        }


        public void init()
        {
            mDir = @"E:\work\work\trunk\client_output\GameData\";
            mTypeName = ".res";
            mTreeView.Nodes.Clear();
            TreeNode node = new TreeNode();            
            int itemNum = build(mDir, node);
            if (itemNum < 1)
            {
                return;
            }
            mTreeView.Nodes.Add(node);
        }

        private int build(string dir, TreeNode node)
        {
            int count = 0;

            foreach (String dirName in Directory.GetDirectories(dir))
            {
                if ((File.GetAttributes(dirName) & FileAttributes.Hidden) != 0)
                    continue;

                string shortName = Path.GetFileName(dirName);
                TreeNode dirNode = new TreeNode(shortName);

                NodeInfo info = new NodeInfo();
                info.fileName = dirName;
                info.type = NodeType.Directory;
                dirNode.Tag = info;

                dirNode.ImageIndex = 1;
                int subCount = build(dirName, dirNode);
                if (subCount > 0)
                {
                    count += subCount;
                    node.Nodes.Add(dirNode);
                }
            }

            foreach (String filename in Directory.GetFiles(dir))
            {
                //跳过隐藏文件夹
                if ((File.GetAttributes(filename) & FileAttributes.Hidden) != 0)
                    continue;

                string ext = Path.GetExtension(filename).ToLower();
                if (!ext.Equals(mTypeName))
                    continue;

                string effectFileName = Path.ChangeExtension(Path.GetFileName(filename), null);    
                TreeNode childNode = new TreeNode(effectFileName);

                NodeInfo info = new NodeInfo();
                info.fileName = filename;
                info.type = NodeType.Effect;
                childNode.Tag = info;

                childNode.ImageIndex = 0;
                node.Nodes.Add(childNode);                

                ++count;
            }


            return count;
        }


        public string mTypeName;
        public string mDir;
        public System.Windows.Forms.TreeView mTreeView;


    }
}
